<?php

require_once dirname(__FILE__) . '/../util/code_util.class.php';

/**
 * This classes is used to patch Mahara during the installation procedure.
 * It works by renaming BlockInstance to BlockInstanceBase so that it
 * can be replaced by the version present in lib2.php.
 * 
 * @copyright (c) 2011 University of Geneva
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
 * @author Laurent Opprecht
 */
class FeedbackPatch{
    
    /**
     * Patch Mahara files to support feedbacks. Function is repeatable.
     */
    public static function apply()
    {
        self::remove(); //ensure that the function is repeatable.
        
        $path = dirname(__FILE__) . '/../../../blocktype/lib.php';
        $content = file_get_contents($path);
        $content = CodeUtil::class_rename('BlockInstance', 'BlockInstanceBase', $content);
        $content = CodeUtil::file_append("require_once '/../artefact/feedback/patch/lib2.php';", $content);

        file_put_contents($path, $content);
    }

    /**
     * Unpatch Mahara files. Function is repeatable.
     */
    public static function remove()
    {
        $path = dirname(__FILE__) . '/../../../blocktype/lib.php';
        $content = file_get_contents($path);

        $content = CodeUtil::class_rename('BlockInstanceBase', 'BlockInstance', $content);
        $content = CodeUtil::file_remove("require_once '/../artefact/feedback/patch/lib2.php';", $content);
        file_put_contents($path, $content);
    }
}
?>
